<template>
  <div class="subapplicant-content">
    <el-collapse v-model="activeNames">
      <el-collapse-item name="1" class="transform-down">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title"
            >文件编号：{{
              modeltype == "circulation"
                ? requestForm.nonContractSealCode
                : modeltype == "contract"
                ? requestForm.contractCode
                : modeltype == "sealcontract"
                ? requestForm.purchaseContractCode
                : modeltype == "serveContract"
                ? requestForm.intellectualPropertyCode
                : ""
            }}</span
          >
        </template>
        <div class="textcontent">
          <div class="item">
            <div class="item-title">申请类型:</div>
            <div class="item-text">
              合同用印{{
                modeltype == "circulation"
                  ? requestForm.nonContractTypeName
                  : modeltype == "contract"
                  ? requestForm.contractKindName
                  : modeltype == "sealcontract"
                  ? requestForm.contractTypeName
                  : modeltype == "serveContract"
                  ? requestForm.contractTypeName
                  : ""
              }}
            </div>
          </div>
          <div class="item">
            <div class="item-title">申请时间:</div>
            <div class="item-text">
              {{
                modeltype == "circulation"
                  ? requestForm.applyTime
                  : modeltype == "contract"
                  ? requestForm.createTime
                  : modeltype == "sealcontract"
                  ? requestForm.applicantDate
                  : modeltype == "serveContract"
                  ? requestForm.applicantDate
                  : ""
              }}
            </div>
          </div>
          <div class="item">
            <div class="item-title">申请部门:</div>
            <div class="item-text">{{ requestForm.deptName }}</div>
          </div>
          <div class="item">
            <div class="item-title">
              {{ modeltype == "circulation" ? "文件类型:" : "合同类型:" }}
            </div>
            <div class="item-text">
              {{
                modeltype == "circulation"
                  ? requestForm.fileTypeNameName
                  : modeltype == "contract"
                  ? requestForm.contractKindName
                  : modeltype == "sealcontract"
                  ? requestForm.contractTypeName
                  : modeltype == "serveContract"
                  ? requestForm.contractTypeName
                  : ""
              }}
            </div>
          </div>
          <div class="item">
            <div class="item-title">应归档份数:</div>
            <div class="item-text">
              {{ requestForm.pigeonholeNumber || "--" }}
            </div>
          </div>
        </div>
      </el-collapse-item>

      <el-collapse-item name="2" class="transform-down">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">归档情况</span>
        </template>
        <div class="form">
          <el-form
            :model="contractForm"
            ref="contractFormRef"
            :rules="queryRules"
            label-width="110px"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="原件归档：" prop="originArchive">
                  <el-radio-group
                    v-model="contractForm.originArchive"
                    :disabled="pagestatus == 'detail'"
                  >
                    <el-radio :label="1">是，已归档</el-radio>
                    <el-radio :label="2">无，未归档</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="扫描件归档：" prop="scanArchive">
                  <el-radio-group
                    v-model="contractForm.scanArchive"
                    :disabled="pagestatus == 'detail'"
                  >
                    <el-radio :label="1">是，已归档</el-radio>
                    <el-radio :label="2">无，未归档</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="档案柜：" prop="space">
                  <el-input
                    v-model="contractForm.space"
                    placeholder="请填写"
                    maxlength="50"
                    :disabled="pagestatus == 'detail'"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="备注：" prop="remark">
                  <el-input
                    type="textarea"
                    v-model="contractForm.remark"
                    placeholder="请填写原因"
                    maxlength="500"
                    :disabled="pagestatus == 'detail'"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
      <Custotdiandetails
        @close="close"
        @submitAuditing="submitAuditing"
        :detaildataArray="requestForm.archiveFileList"
        :pagestatus="pagestatus"
        :pigeonholeNumber="requestForm.pigeonholeNumber"
      ></Custotdiandetails>
    </el-collapse>
  </div>
</template>

<script setup>
import Custotdiandetails from "./custotdiandetails";
import { nonConSealCheck } from "@/api/seal/circulation";
import { checkContractApi } from "@/api/sales/contract";
import { checkSealApi } from "@/api/purchase/contractSeal";
import { checkSealApi as contractSealApi } from "@/api/serve/contractSeal";
import { editsaveArchive } from "@/api/seal/placeonfile";
import { ref, onMounted, provide, inject } from "vue";

import { ElMessage, ElMessageBox } from "element-plus";

const emits = defineEmits(["closeauditDialog1"]);

const props = defineProps({
  // 合同数据
  requestForm: {
    type: Object,
    default: {},
  },
  pagestatus: {
    type: String,
    default: "",
  },
  modeltype: {
    type: String,
    default: "circulation",
  },
});
const getNonContractual = inject("getNonContractual");
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3"]);
const contractForm = ref({});
const queryRules = {
  originArchive: [
    { required: true, message: "请选择原件归档", trigger: "change" },
  ],
  scanArchive: [
    { required: true, message: "请选择扫描件归档", trigger: "change" },
  ],
  space: [{ required: true, message: "请填写档案柜", trigger: "change" }],
};

let contractFormRef = ref(null);

/**
 * @description: 提交审核
 */
const submitAuditing = async (archiveFileList) => {
  await contractFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定提交吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          if (props.pagestatus == "edit") {
            let tatol = archiveFileList.reduce(
              (total, item) => total + item.realCount * 1,
              0
            );
            let params = {
              auditRemark: "合同归档",
              nonContractSealId: props.requestForm.nonContractSealId,
              checkStatus: 3,
              archiveFileList,
              archiveVo: contractForm.value,
              flowIntype: props.requestForm.nextAudit.flowIntype,
              shouldCountAll: props.requestForm.pigeonholeNumber,
              realCountAll: tatol,
            };
            switch (props.modeltype) {
              case "circulation":
                params.nonContractSealId = props.requestForm.nonContractSealId;
                break;
              case "contract":
                params.contractId = props.requestForm.contractId;
                break;
              default:
                break;
            }
            editsaveArchive(params).then((res) => {
              if (res.code == 200) {
                ElMessage.success("操作成功");
              }
            });
          } else {
            let tatol = archiveFileList.reduce(
              (total, item) => total + item.realCount * 1,
              0
            );
            console.log("tatol", tatol);
            let params = {
              auditRemark: "合同归档",
              checkStatus: 3,
              archiveFileList,
              archiveVo: contractForm.value,
              flowIntype: props.requestForm.nextAudit.flowIntype,
              shouldCountAll: props.requestForm.pigeonholeNumber,
              realCountAll: tatol,
            };
            let Func;
            switch (props.modeltype) {
              case "circulation":
                Func = nonConSealCheck;
                params.nonContractSealId = props.requestForm.nonContractSealId;
                break;
              case "contract":
                Func = checkContractApi;
                params.contractId = props.requestForm.contractId;
                params.contractKind = props.requestForm.contractKind;
                break;
              case "sealcontract":
                Func = checkSealApi;
                params.contractCheckStatus = 3;
                params.purchaseContractId =
                  props.requestForm.purchaseContractId;
                params.purchaseOrderId = props.requestForm.purchaseOrderId;
                params.purchaseInqMaterialDetailLitsRespVos =
                  props.requestForm.purchaseInqMaterialDetailLitsRespVos;
                break;
              case "serveContract":
                Func = contractSealApi;
                params.contractCheckStatus = 3;
                params.synthesisContractId =
                  props.requestForm.synthesisContractId;
                params.contractType = props.requestForm.contractType;
                break;
              default:
                break;
            }
            Func(params).then((res) => {
              if (res.code == 200) {
                ElMessage.success("操作成功");
                emits("closeauditDialog1");
                emits("handleBack");
                getNonContractual();
              }
            });
          }
        })
        .catch(() => {});
    } else {
      console.log("error submit!");
    }
  });
};

const close = () => {
  emits("closeauditDialog1");
};

/**
 * @description: 回显详情
 */
const handlerdetail = () => {
  console.log("requestForm", props.requestForm);
  contractForm.value = props.requestForm.archiveVo;
};
defineExpose({
  handlerdetail,
});
</script>

<style scoped lang="scss">
.subapplicant-content {
  height: 650px;
  .textcontent {
    padding: 0px 20px;
    margin: 10px 0;
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    background-color: #f0f0f0;
    .item {
      display: flex;
      width: 33%;
      height: 50px;
      line-height: 50px;
      .item-title {
        font-size: 14px;
        font-weight: 700;
        margin-right: 10px;
      }
    }
  }
  .form {
    margin-top: 10px;
  }
}
.subapplicant-content {
  overflow-y: scroll;
}
</style>
